package com.qqtop10.manager.controller;

import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qqtop10.manager.model.IntCategory;
import com.qqtop10.manager.model.PagingArgs;
import com.qqtop10.manager.model.Result;
import com.qqtop10.manager.service.IntCategoryService;
import com.qqtop10.manager.utils.RandomUtils;

/**
 * 
 * @author Jovi
 *
 */
@RequestMapping("/manager/intcategory")
@Controller
public class IntCategoryController extends BaseController {
	@Autowired
	@Qualifier("intCategoryService")
	private IntCategoryService intCategoryService;

	@RequestMapping("all.do")
	@ResponseBody
	public Result all(IntCategory input) {
		try {
			PagingArgs pagingArgs = getPagingArgs();
			return page(intCategoryService.count(input), intCategoryService.all(input, pagingArgs));
		} catch (Exception e) {
			return excep(e);
		}
	}

	/**
	 * @param name
	 * @return
	 */
	@RequestMapping("add.do")
	@ResponseBody
	public Result add(String name) {
		IntCategory intCategory = new IntCategory();
		intCategory.setUuid(RandomUtils.makeUUID());
		intCategory.setCreatetime(new Date());
		intCategory.setCreator(super.getLogindUuid());
		intCategory.setName(name);
		intCategoryService.insertSelective(intCategory);
		return ok("操作成功！");
	}

	@RequestMapping("delete.do")
	@ResponseBody
	public Result delete(int[] ids) {
		try {
			IntCategory input = new IntCategory();
			input.setMender(getLogindUuid());
			input.setUpdatetime(now());
			int count = 0;
			for (int i = 0; i < ids.length; i++) {
				input.setId(ids[i]);
				if (intCategoryService.delete(input) < 1) {
					ids[count] = ids[i];
					++count;
				}
			}
			if (count == 0) {
				return ok("操作成功！");
			} else if (count < ids.length) {
				return ok("操作已完成。但有些数据没有删除成功。");
			} else {
				return err("全部操作失败！");
			}
		} catch (Exception e) {
			return excep(e);
		}
	}

	@RequestMapping("addRecommendGoods.do")
	@ResponseBody
	public Result updateRecommendGoods() {
		
		return ok("操作成功");
	}
}
